package com.hp.bon.sgw.wh.channel.invoices.bizSlot;


import com.alibaba.fastjson.JSON;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.invoices.BizSlot;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: SGW_WH2.1_TMALL
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/8 15:51
 * 账务中心新接口
 * 开票API—issueInvoice
 */
public class IssueInvoiceSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory
            .getLogger(IssueInvoiceSlot.class);

    @Override
    public BizResult doBiz(BizParam bizParam) {

        LOG.info("start new  IssueInvoice......");
        BizResult bizResult = new BizResult();
        //       String reqBody = bizParam.getParamString(BizConsts.KEY_REQUEST_BODY);
//        Map<String, Object> reqBodyMap = HttpClientUtils.parseJSON2Map(reqBody);
//        String issueId= (String) reqBodyMap.get(BizConsts.RQUEST_KEY_ISSUERID);
//        String type= (String) reqBodyMap.get(BizConsts.RQUEST_KEY_TYPE);
//        String lanId= (String) reqBodyMap.get(BizConsts.RQUEST_KEY_ISSUERID);
//        JSONArray objects = JSONArray.fromObject(reqBodyMap.get(BizConsts.RQUEST_KEY_OBJECTS));
        HashMap<String, Object> msgBody = new HashMap<>();
        String issueId = bizParam.getParamString(BizConsts.RQUEST_KEY_ISSUERID);
        String type = bizParam.getParamString(BizConsts.RQUEST_KEY_TYPE);
        String lanId = bizParam.getParamString(BizConsts.KEY_INVOICE_LANID);
        JSONArray objects = JSONArray.fromObject(bizParam.getParam(BizConsts.RQUEST_KEY_OBJECTS));

        msgBody.put(BizConsts.RQUEST_KEY_ISSUERID, issueId);
        msgBody.put(BizConsts.KEY_INVOICE_LANID, lanId);

        //获取URL地址
        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

        List<Map<String, String>> objList = new ArrayList<Map<String, String>>();

        for (int i = 0; i < objects.length(); i++) {
            HashMap<String, String> objMap = new HashMap<>();
            JSONObject s1 = objects.getJSONObject(i);
            String id = (String) s1.get("id");
            String productId = (String) s1.get("productId");
            String billMonth = "";
            if (type.equals("1000")) {
                billMonth = (String) s1.get("billMonth");
            }
            String amount = (String) s1.get("amount");
            String status = (String) s1.get("status");
            String invoiceId = (String) s1.get("invoiceId");
            objMap.put("amount", amount);
            objMap.put("billMonth", billMonth);
            objMap.put("id", id);
            objMap.put("productId", productId);
            objMap.put("status", status);
            objList.add(objMap);
            //invoiceId
            msgBody.put("invoiceId",invoiceId);
        }
        msgBody.put(BizConsts.RQUEST_KEY_OBJECTS, objList);
        msgBody.put(BizConsts.RQUEST_KEY_TYPE, type);
        String msgBodyStr = JSON.toJSONString(msgBody);

        String respStr = HttpClientUtils.postHttpStr(msgBodyStr, url,bizParam);
        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            respStr=  HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL,"","","");
        }else {

            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        }
        LOG.info("Responding to external messages====:{}",respStr);
        bizResult.setHttpBody(respStr);
        return bizResult;
    }
}
